import { Component, Input, OnInit } from '@angular/core';
import { CakeOrderViewComponent } from '../order-view/cake-order-view.component';
import { Router } from '@angular/router';
import { MyModalHelper } from '@core/my-modal.helper';
import { myValidNull } from '@core/util/string-utils';
import { ServeBackComponent } from '../../serve-back/serve-back.component';
import { MoonCakeService } from '@core/routes-services/moon-cake.service';
import { OrderLogisticsComponent } from '../../../order-logistics/order-logistics.component';
import { AuditingComponent } from '../../../auditing/auditing.component';
import {
  CAKE_ORDER_SERVICE_STATUS_OPTION,
  CAKE_ORDER_STATUS_OPTION,
  CAKE_ORDER_VOUCHER_STATUS_OPTION, PAYMENT_METHOD_OPTION,
} from '../../../../constant/dictionary';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'all-order-list',
  templateUrl: './all-order-list.component.html',
})
export class MidAutumnAllOrderListComponent implements OnInit {


  isLoading = false;
  keyword: any;
  productId: any;
  method: any = null;
  @Input() allProducts: any [] = [];
  @Input() cakeOrderStatusOption: any [] = CAKE_ORDER_STATUS_OPTION;
  @Input() cakeOrderServiceStatusOption: any [] = CAKE_ORDER_SERVICE_STATUS_OPTION;
  @Input() cakeOrderVoucherStatusOption: any [] = CAKE_ORDER_VOUCHER_STATUS_OPTION;

  paymentMethod: any [] = PAYMENT_METHOD_OPTION;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  status: any;
  serviceStatus: any;
  voucherStatus: any;

  constructor(private message: NzMessageService,
              private router: Router,
              private moonCakeService: MoonCakeService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  sortMap = {
    payTime: 'descend',
    saveTime: null,
  };
  sortName = null;
  sortValue = null;

  sort(sortName: string, value: string): void {
    this.sortName = sortName;
    this.sortValue = value ? value : 'ascend';
    for (const key in this.sortMap) {
      this.sortMap[key] = null;
      this.sortMap[key] = (key === sortName ? this.sortValue : null);
    }
    this.search();
  }

  initPage() {
    this.isLoading = true;
    this.moonCakeService.pagination(this.page, this.keyword, this.method, this.status, this.serviceStatus, this.productId, this.voucherStatus, this.sortName, this.sortValue).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  seeEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    let statusOption = this.cakeOrderStatusOption;
    if (entity.paymentMethod == 2) {
      statusOption = this.cakeOrderServiceStatusOption;
    }
    if (entity.paymentMethod == 3 ||entity.paymentMethod == 4) {
      statusOption = this.cakeOrderVoucherStatusOption;
    }
    this.modalHelper.open(CakeOrderViewComponent, {
      entity: bak,
      allProducts: this.allProducts,
      statusOption: statusOption,
    }, 'lg', {
      nzTitle: '订单信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  logistics = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OrderLogisticsComponent, { entity: bak, businessService: this.moonCakeService }, 520, {
      nzTitle: '物流信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  auditing = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(ServeBackComponent, {
      entity: bak,
      businessService: this.moonCakeService,
      allProducts: this.allProducts,
    }, 660, {
      nzTitle: '联系大客户情况',
    }).subscribe(result => {
      if (result) {
        this.message.success('保存成功');
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };


  payAuditing = (entity) => {
    this.moonCakeService.confirmPayAuditing(entity.id).then((data) => {
      this.changePageNo(this.page.number);
      this.message.success('操作成功');
    }, error => console.log('操作出错，后续处理'));
  };

  rejectPayAuditing = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(AuditingComponent, { entity: bak,businessService: this.moonCakeService}, 360, {
      nzTitle: '驳回凭证',
    }).subscribe(result => {
      if(result){
        this.message.success("驳回成功")
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  picList: any [] = [];
  isShowPic: boolean = false;

  showPic(key) {
    this.picList = [];
    this.picList.push({ imgKey: key });
    this.isShowPic = true;
  }

  onPicClose(res) {
    this.isShowPic = res;
    this.isShowPic = false;
  }
}
